﻿using Sc.Domain;
using Sc.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace Sc.Web.Controllers
{
    public class CarInsuranceController : BaseController
    {
        // GET: CarInsurance
        public ActionResult Index()
        {
            ViewData["olist"] = CommService.GetOrgzByUserPer(UserLogin.UserId, 3);
            return View();
        }

        /// <summary>
        /// 获取保险列表
        /// </summary>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="pageIndex">当前页</param>
        /// <returns></returns>
        public JsonResult GetPageData(int pageSize, int pageIndex)
        {
            var orgz = Convert.ToInt32(Request.Params["orgz"]);
            var start = Request.Params["start"];
            var end = Request.Params["end"];
            var data = new Car_InsuranceService().GetList(orgz, pageSize, pageIndex, start, end);

            return Json(data, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 创建保险记录
        /// </summary>
        /// <returns></returns>
        public ActionResult CreateCarInsurance()
        {
            ViewData["type"] = new BaseItemService().GetListByType(11);
            return View();
        }
        /// <summary>
        /// 更新保险记录
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdateCarInsurance()
        {
            int id = Convert.ToInt32(Request.Params["id"]);
            ViewData["ent"] = new Car_InsuranceService().GetDetail(id);
            ViewData["type"] = new BaseItemService().GetListByType(11);
            return View();
        }

        /// <summary>
        /// 保存保险数据
        /// </summary>
        /// <returns></returns>
        public int SaveData()
        {
            //var CarId = Request.Params["CarId"];
            //var TypeId = Request.Params["TypeId"];
            //var InsuranceNo = Request.Params["InsuranceNo"];
            //var InsuranceMoney = Request.Params["InsuranceMoney"];
            //var InsuranceCompany = Request.Params["InsuranceCompany"];
            //var BeginTime = Request.Params["BeginTime"];
            //var EndTime = Request.Params["EndTime"];
            var id = Request.Params["id"];
            //Car_Insurance ent = new Car_Insurance();
            //ent.CarId = int.Parse(CarId);
            //ent.TypeId = int.Parse(TypeId);
            //ent.InsuranceNo = InsuranceNo;
            //ent.InsuranceMoney = decimal.Parse(InsuranceMoney);
            //ent.InsuranceCompany = InsuranceCompany;
            //ent.BeginTime = DateTime.Parse(BeginTime);
            //ent.EndTime = DateTime.Parse(EndTime);
            string data = Request.Params["data"];
            JavaScriptSerializer json = new JavaScriptSerializer();
            Car_Insurance ent = json.Deserialize<Car_Insurance>(data);
            ent.UserId = UserLogin.UserId;
            ent.UserId = UserLogin.UserId;
            if (!string.IsNullOrEmpty(id))
            {
                ent.Id = Convert.ToInt32(id);
                return new Car_InsuranceService().Update(ent) ? 1 : 0;
            }
            return new Car_InsuranceService().Add(ent) ? 1 : 0;
        }
        /// <summary>
        /// 删除保险记录
        /// </summary>
        /// <param name="ids">ids</param>
        /// <returns></returns>
        public int DeleteList(string ids)
        {
            return new Car_InsuranceService().DeleteList(ids) ? 1 : 0;
        }
    }
}